<?php


namespace app\libs\tool;


use app\libs\exception\ServerError;
use think\facade\Cache;

class CacheTool
{

    /**
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws ServerError
     */
    public function setCache($key, $value, $allowTime = 300)
    {
        if (!$key || !$value) {
            throw new ServerError(['msg' => '键值都不能为空']);
        }

        Cache::store('redis')->set($key, $value, $allowTime);
    }

    public function getCache($key)
    {
        return Cache::store('redis')->get($key);
    }

    public function checkCache($key, $real_key)
    {
        $current_cache = $this->getCache($key);

        if (!$current_cache) return '邮箱验证码异常, 请重新发送';

        return strtoupper($this->getCache($key)) === strtoupper($real_key);

    }

    public static function clearCache()
    {
        Cache::store('redis')->clear();
    }
}